﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Net.Utilities
{
    public static class CodeHelper
    {
        public static void AccumulateExceptions<T>(IEnumerable<T> items, Action<T> act)
        {
            AccumulateExceptions(items.Select(item => new Action(() => act(item))));
        }

        public static void AccumulateExceptions(params Action[] actions)
        {
            AccumulateExceptions(actions.AsEnumerable());
        }

        public static void AccumulateExceptions(IEnumerable<Action> actions)
        {
            var exceptions = new List<Exception>();

            foreach (var act in actions)
                try
                {
                    act();
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                }

            if (exceptions.Count > 0)
                throw ExceptionFactory.Multi(exceptions);
        }
    }
}
